package $NAMESPACE.domain.service.impl;

import com.ugeez.commons.account.service.impl.UserServiceImpl;
import com.ugeez.commons.security.vo.UserLoginResult;
import com.ugeez.commons.security.vo.UserSession;
import $NAMESPACE.common.enums.XXXEnum;
import $NAMESPACE.domain.service.AppUserService;
import $NAMESPACE.domain.service.SomeEntityService;
import $NAMESPACE.domain.service.UserRoleService;
import $NAMESPACE.persistence.dao.AppUserMapper;
import $NAMESPACE.persistence.entity.AppUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Slf4j
@Primary
@Service
public class AppUserServiceImpl extends UserServiceImpl<AppUser, AppUserMapper, UserSession, UserLoginResult> implements AppUserService {

    @Autowired
    private SomeEntityService someEntityService;

    @Autowired
    private UserRoleService userRoleService;

    @Transactional
    @Override
    protected AppUser create(String name,
                             String head,
                             String idCardNo,
                             String idCardType,
                             LocalDateTime birthday,
                             String phone,
                             String email,
                             Integer gender,
                             String country,
                             String province,
                             String city,
                             String area,
                             String street,
                             Map<String, Object> biz) {
        AppUser user = super.create(name, head, idCardNo, idCardType, birthday, phone, email, gender, country, province, city, area, street, biz);
		//Todo: 创建用户同时做一些业务，比如创建其它关联数据

        //这里我们假设创建一个关联数据
        someEntityService.create(user.getId(), XXXEnum.KEY1, null);

        return user;
    }

    @Transactional
    @Override
    public AppUser create(String username, String password, List<String> roles) {
        //自定义创建用户方式

        AppUser user = super.create(username, password, null);

        //创建用户时设定用户的角色
        for (String role : roles) {
            userRoleService.addRole(user.getId(), role);
        }

        return user;
    }
}
